package com.design_patterns.singleton;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: 李德才
 * @description:
 * @create: 2020-11-15 18:08
 **/
public class Lock {
    private static Lock instance = null;
    private static ReentrantLock lock = new ReentrantLock();

    private Lock() {}

    public static Lock getInstance() {
        if(instance == null) {
            lock.lock(); // 显式调用，手动加锁
            if(instance == null) {
                instance = new Lock();
            }
            lock.unlock(); // 显式调用，手动解锁
        }
        return instance;
    }


    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() ->{
                System.err.println(Lock.getInstance().hashCode());
            }).start();
        }
    }
}
